var that = {};
/**
 * 获取当前用户信息，如果不存在则返回null
 */
that.getUser = function () {
    if (sessionStorage.getItem("user")) {
        try {
            return JSON.parse(sessionStorage.getItem("user"));
        } catch (ex) {
            console.error("sessionStorage中存储的user信息有问题");
            return null;
        }
    } else {
        return null;
    }
}

/**
 * 设置当前用户，如果传入参数为空则是退出登录
 */
that.setUser = function (user) {
    if (user == null) {
        sessionStorage.removeItem("user");
    } else {
        sessionStorage.setItem("user", JSON.stringify(user));
    }
}

/**
 * 查找当前路由所在的对象，并返回
 * @param {String} curPath  当前路由
 * @param {Array} list      路由列表
 * @return {Object} 
 */
that.findnav = function (curPath, list) {
    return list.find((v) => {
        if (v.submenu) {
            return that.findnav(curPath, v.submenu);
        } else {
            return v.uri == curPath;
        }
    });
}

/** 匹配当前导航的对象
 * @param  {String} curPath  当前路由
 * @param  {Array}  navList  路由列表
 * @return {Object} curNav   当前路由的对象 如： {index": "1", "text": "首页", "uri": "/"}
 */
that.matchCurNav = function (curPath, navlist) {
    let curNav;
    let _navlist = navlist || [];
    _navlist.forEach((v, i) => {
        if (v.uri && v.uri === curPath) {
            curNav = v;
        } else if (v.submenu && that.matchCurNav(curPath, v.submenu)) {
            curNav = that.matchCurNav(curPath, v.submenu);
        }
    });
    return curNav;
}

/** 
 * 判断浏览器是否支持某一个CSS3属性 
 * @param {String} 属性名称 
 * @return {Boolean} true/false 
 * @version 1.0 
 * @author ydr.me 
 * 2014年4月4日14:47:19 
 */
that.supportCss3 = function (style) {
    var prefix = ['webkit', 'Moz', 'ms', 'o'],
        i,
        humpString = [],
        htmlStyle = document.documentElement.style,
        _toHumb = function (string) {
            return string.replace(/-(\w)/g, function ($0, $1) {
                return $1.toUpperCase();
            });
        };

    for (i in prefix)
        humpString.push(_toHumb(prefix[i] + '-' + style));

    humpString.push(_toHumb(style));

    for (i in humpString)
        if (humpString[i] in htmlStyle) return true;

    return false;
}
// that.supportCss3('flex') ? '' : location.href="/browserUpdate.html";
module.exports = that;